﻿//#region Copyright
////       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
//// 
////        This program is free software: you can redistribute it and/or modify
////        it under the terms of the GNU General Public License as published by
////        the Free Software Foundation, either version 3 of the License, or
////        (at your option) any later version.
//// 
////        This program is distributed in the hope that it will be useful,
////        but WITHOUT ANY WARRANTY; without even the implied warranty of
////        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
////        GNU General Public License for more details.
//// 
////        You should have received a copy of the GNU General Public License
////        along with this program.  If not, see <http://www.gnu.org/licenses/>.
//#endregion
//using System;
//using System.Runtime.CompilerServices;
//using System.Web;
//using FT.Architecture.Repositories;
//using FT.Architecture.Logging;
//using FT.Architecture.UnitOfWork;

//namespace FT.Architecture.NHibernate.UnitOfWork
//{
//    /// <summary>
//    /// <see cref="NHibernate"/> Thread Safe Unit of Work <see cref="IHttpModule"/>
//    /// </summary>
//    public class NHibernateUnitOfWorkHttpModule : IHttpModule
//    {
        
//        private readonly string[] _requiredExtensions = new []
//        {
//            "aspx",
//            "ashx",
//            "ascx"                                          
//        };

//        public HttpContext Context
//        {
//            get
//            {
//                return HttpContext.Current;
//            }
//        }

//        /// <summary>
//        /// Initializes a module and prepares it to handle requests.
//        /// </summary>
//        /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application </param>
//        public void Init(HttpApplication context)
//        {
//            Log.UnitOfWork.Debug("Initialising the UnitOfWork Http Module");
//            context.BeginRequest    += BeginRequest;
//            context.EndRequest      += EndRequest;
//            Log.UnitOfWork.Debug("Initialising the UnitOfWork Http Module (end)");
//        }

//        private bool IsAuthorised(string filePath)
//        {
//            foreach (string extension in _requiredExtensions)
//            {
//                if (filePath.EndsWith(extension))
//                    return true;
//            }

//            return false;
//        }

//        [MethodImpl(MethodImplOptions.Synchronized)]
//        private void BeginRequest(object sender, EventArgs e)
//        {
            
//            //Filters the extensions that requires NHibernate to be loaded
//            if (!IsAuthorised(Context.Request.FilePath))
//            {
//                Log.UnitOfWork.Debug(string.Format("UoW Module: Begin request ignored ({0})",
//                                                   Context.Request.FilePath));
//                return;
//            }

//            Log.UnitOfWork.Debug("Beginning a UoW HttpModule request");
//            IUnitOfWork uow = BaseDal<IUnitOfWork>.GetUnitOfWork();
//            Log.UnitOfWork.Debug("Beginning a UoW HttpModule request " + uow.Identifier);
        
//        }

//        [MethodImpl(MethodImplOptions.Synchronized)]
//        private void EndRequest(object sender, EventArgs e)
//        {
           
//            //Filters the extensions that requires NHibernate to be loaded
//            if (!IsAuthorised(Context.Request.FilePath))
//            {
//                Log.UnitOfWork.Debug(string.Format("UoW Module: End request ignored ({0})",
//                                                   Context.Request.FilePath));
//                return;
//            }

//            Log.UnitOfWork.Debug("Ending a UoW HttpModule request");

//            Exception exception = Context.Server.GetLastError();

//            IUnitOfWork unitOfWork = BaseDal<IUnitOfWork>.HasCurrentUnitOfWork 
//                ? BaseDal<IUnitOfWork>.Current 
//                : null;

//            if (unitOfWork != null)
//            {
//                if (exception == null) //Nothing went wrong, we commit
//                {
//                    Log.UnitOfWork.Debug("Committing a UoW HttpModule request " + unitOfWork.Identifier);
//                    unitOfWork.Commit();
//                    Log.UnitOfWork.Debug("Disposing a UoW HttpModule request in EndRequest() " +
//                                         unitOfWork.Identifier);
//                    unitOfWork.Dispose();
//                }
//                else //An exception was raised at some point, we rollback immediatly
//                {
//                    Log.UnitOfWork.Debug(
//                        "Rollbacking a UoW HttpModule request because of exception " + unitOfWork.Identifier,
//                        exception);
//                    unitOfWork.Rollback();
//                    Log.UnitOfWork.Debug("Disposing a UoW HttpModule request in EndRequest() " +
//                                         unitOfWork.Identifier);
//                    unitOfWork.Dispose();
//                }
//            }
//            else
//            {
//                Log.UnitOfWork.Debug("The Unit of Work was null for this page");
//            }
               
            
//        }

//        /// <summary>
//        /// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
//        /// </summary>
//        public void Dispose()
//        {
//            IUnitOfWork unitOfWork = BaseDal<IUnitOfWork>.HasCurrentUnitOfWork
//               ? BaseDal<IUnitOfWork>.Current
//               : null;

//            Log.UnitOfWork.Debug("Disposing the HttpModule (UoW)");
//            if (unitOfWork != null)
//            {
//                Log.UnitOfWork.Debug("Disposing a UoW HttpModule request in Dispose() " + unitOfWork.Identifier);
//                unitOfWork.Dispose();
//            }
//        }
//    }
//}